<?php
/**
 * Created by PhpStorm.
 * User: yangchunrun
 * Date: 17/3/20
 * Time: 下午10:26
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

class plugin_thinker_wx
{
}

class plugin_thinker_wx_forum extends plugin_thinker_wx
{
    function viewthread_posttop_output()
    {
        global $_G, $postlist;
        $splugin_setting = $_G['cache']['plugin']['thinker_wx'];
        if (!in_array($_G['fid'], unserialize($splugin_setting['is_qrcode_down']))) {
            return array();
        }
//        if (!in_array($_G['groupid'], unserialize($splugin_setting['is_free_credits']))) {
//            return array();
//        }

        foreach ($postlist as $id => $post) {   //Each Post
        //*****************************************************End NetDisk attach
        //*****************************************************Free post attch
            $num = preg_match_all("/\"forum.php\?mod\=attachment.*\" /U", $postlist[$id]["message"], $match);
            if ($num != 0) {//Post Attach
                foreach ($match[0] as $match_id => $href) { //Each Post Attach
                    $mid = explode("aid=", $href);
                    $aidcode = explode('"', $mid[1]);
                    $aidcode = $aidcode[0];
                    $aid = base64_decode($aidcode);
                    $aid = explode("|", $aid);
                    $aid = $aid[0];
                    foreach ($post["attachments"] as $a_id => $attachment) {//Search Attach Information
                        $a_id = $attachment["aid"];
                        if ($a_id == $aid) {
                            $inf = "attachment" . $is_archive . "|" . $aidcode . "|" . $attachment["ext"] . "|" . $attachment["filesize"];
                            $inf = $inf . "|" . $attachment["downloads"] . "|" . urlencode($attachment["filename"]);
                            $pay_href = "";
                            $need_pay = 0;
                            $inf = $inf . "|" . $need_pay . "|" . $pay_href.'|'.$a_id;
                            $inf = base64_encode($inf);
                            break;
                        }

                    }//END Search Attach Information
                    $Myhref = '"plugin.php?id=thinker_wx:qrcode&inf=' . $inf . '"';
                    $postlist[$id]["message"] = preg_replace("/\"forum.php\?mod\=attachment.*?aid\=" . $aidcode . "\" /", $Myhref, $postlist[$id]["message"]);//Replace Href
                }//End Each Post Attach

            }//End Post Attach
//End free post attch
//***********************************************************Need pay post attch
            $num = preg_match_all("/\"forum.php\?mod\=misc.*?action=attachpay.*?\" /", $postlist[$id]["message"], $match);
            if ($num != 0) {//Post Attach
                foreach ($match[0] as $match_id => $href) { //Each Post Attach
                    $mid = explode("aid=", $href);
                    $aid = explode('&', $mid[1]);
                    $aid = $aid[0];
                    foreach ($post["attachments"] as $a_id => $attachment) {//Search Attach Information
                        if ($attachment["aid"] == trim($aid)) {
                            $inf = "attachment" . $is_archive . "|" . $aidcode . "|" . $attachment["ext"] . "|" . $attachment["filesize"];
                            $inf = $inf . "|" . $attachment["downloads"] . "|" . urlencode($attachment["filename"]);
                            $need_pay = 1;
                            $pay_href = "forum.php?mod=misc&action=attachpay&aid=" . $attachment["aid"] . "&tid=" . $attachment["tid"] . "";
                            $inf = $inf . "|" . $need_pay . "|" . $pay_href.'|'.$a_id;
                            $inf = base64_encode($inf);
                            break;
                        }

                    }//END Search Attach Information
                    $Myhref = '"plugin.php?id=dxksst_download:download&inf=' . $inf . '">';
                    $postlist[$id]["message"] = preg_replace("/\"forum.php\?mod\=misc.*?action=attachpay.*?aid=" . $aid . ".*\"\>/", $Myhref, $postlist[$id]["message"]);//Replace Href
                }//End Each Post Attach
            }//End Post Attach
//**************************************************End need pay post attch
            if (!empty($post["attachments"])) {//Common Attach
                foreach ($post["attachments"] as $a_id => $attachment) {//Each attach
                    $inf = "attachment" . $is_archive . "|" . packaids($attachment) . "|" . $attachment["ext"] . "|" . $attachment["filesize"];
                    $inf = $inf . "|" . $attachment["downloads"] . "|" . urlencode($attachment["filename"]);
                    $need_pay = 0;
                    if ($attachment["price"] && !$attachment["payed"]) {  // Need Pay Attach
                        $need_pay = 1;
                        $postlist[$id]["attachments"][$a_id]["payed"] = 1;
                        $pay_href = "forum.php?mod=misc&action=attachpay&aid=" . $attachment["aid"] . "&tid=" . $attachment["tid"] . "";
                    }//END Need Pay Attach
                    $inf = $inf . "|" . $need_pay . "|" . $pay_href.'|'.$a_id;
                    $inf = base64_encode($inf);
                    $str = <<<EOT
<script type="text/javascript">

  var dxksst_a=document.getElementsByTagName('a');
  var num=dxksst_a.length-1;
  var dxksst_b=dxksst_a[num];
   dxksst_b.href='javascript:;';
  dxksst_b.onclick=function()
  {
 showWindow('test','plugin.php?id=thinker_wx:qrcode&inf=$inf','get',0)
  }

</script>
EOT;
                    ?><?php
                    $postlist[$id]["attachments"][$a_id]["filename"] = $postlist[$id]["attachments"][$a_id]["filename"] . $str;
                }//END Each Attach
            }//END Common Attach
        }//END Each Post
        return;
    }
}